# 维修价格模型
from .base import BaseModel
from .database import db
from datetime import datetime

class RepairPrice(BaseModel):
    __tablename__ = 'repair_price'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='主键ID')
    item_name = db.Column(db.String(100), nullable=False, comment='维修物品名称（如电视、洗衣机等）')
    price = db.Column(db.Numeric(10, 2), nullable=False, comment='维修价格')
    description = db.Column(db.String(255), nullable=True, comment='价格说明或备注')
    created_at = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, comment='更新时间')
    is_deleted = None

    # 重写to_dict方法，排除is_deleted字段，并处理价格格式
    def to_dict(self):
        result = super().to_dict()
        result.pop('is_deleted', None)
        # 将Numeric类型的价格转换为字符串或浮点数
        if 'price' in result and result['price'] is not None:
            result['price'] = float(result['price'])
        return result

    def __repr__(self):
        return f'<RepairPrice {self.item_name}: {self.price}>'